clear

clear matrix
clear mata
set more off
set maxvar 9000
use "C:\Users\Andy baker\OneDrive - UCB-O365\Stata\Barry\Final\OmnibusBoth.dta", clear
fillin id wave
xtset id wave
recode respond .=0
drop if suspect==1
drop if whennew==2

recode v5d_econpastbrazil 1=5 2=4 3=3 4=2 5=1 8=. 9=.

replace v74_disc1named =v74_disc1named[_n-1] if wave==3 | wave==6
replace v75_disc2named =v75_disc2named[_n-1] if wave==3 | wave==6 
replace v76_disc3named =v76_disc3named[_n-1] if wave==3 | wave==6

rename v74_disc1named discnamed1
rename v74b_disc1relate discrelate1
rename v74c_disc1presvote discpresvote1 
rename v74dcs_disc1govvote cs_discgovvote1
rename v74djf_disc1govvote jf_discgovvote1

rename v75_disc2named discnamed2
rename v75b_disc2relate discrelate2
rename v75c_disc2presvote discpresvote2 
rename v75dcs_disc2govvote cs_discgovvote2
rename v75djf_disc2govvote jf_discgovvote2

rename v76_disc3named discnamed3
rename v76b_disc3relate discrelate3
rename v76c_disc3presvote discpresvote3 
rename v76dcs_disc3govvote cs_discgovvote3
rename v76djf_disc3govvote jf_discgovvote3

*Threshold
mvdecode v41a_thermlula* v41c_thermciro* v41d_thermgaro* v41e_thermserra* v41x_thermalckmin* v41y_thermheloisa* v41z_thermbuarque* v42a_pidthermpsdb* v42c_pidthermpt* v42e_pidthermpsb* v42f_pidthermpps* v41b_thermfhc* v41f_thermroseana*, mv(18)
mvdecode v41a_thermlula* v41c_thermciro* v41d_thermgaro* v41e_thermserra* v41x_thermalckmin* v41y_thermheloisa* v41z_thermbuarque* v42a_pidthermpsdb* v42c_pidthermpt* v42e_pidthermpsb* v42f_pidthermpps* v41b_thermfhc* v41f_thermroseana*, mv(19)

	*For Lula voters
egen tempciroserraag=rowmean(v41c_thermciro v41d_thermgaro v41e_thermserra)
egen templula=rowmean(v41a_thermlula)
gen lula_therm2=templula-tempciroserraag if wave==2
gen lula_threshold2=(lula_therm2+10)/20

	*For Ciro voters
egen templulaserraag=rowmean(v41a_thermlula v41d_thermgaro v41e_thermserra)
egen tempciro=rowmean(v41c_thermciro)
gen ciro_therm2=tempciro-templulaserraag if wave==2
gen ciro_threshold2=(ciro_therm2+10)/20

	*For Serra voters
egen templulaagciro=rowmean(v41a_thermlula v41d_thermgaro v41c_thermciro)
egen tempserra=rowmean(v41e_thermserra)
gen serra_therm2=tempserra-templulaagciro if wave==2
gen serra_threshold2=(serra_therm2+10)/20

	*For AG voters
egen templulaserraciro=rowmean(v41a_thermlula v41e_thermserra v41c_thermciro)
egen tempag=rowmean(v41d_thermgaro)
gen ag_therm2=tempag-templulaserraciro if wave==2
gen ag_threshold2=(ag_therm2+10)/20


*2006  
	*For Lula voters
egen tempalckheloisa=rowmean(v41x_thermalckmin v41y_thermheloisa)
egen templula5=rowmean(v41a_thermlula )
gen lula_therm5=templula5-tempalckheloisa if wave==5
gen lula_threshold5=(lula_therm5+10)/20

	*For Alckmin voters
egen templulaheloisa=rowmean(v41a_thermlula v41y_thermheloisa)
egen tempalckmin=rowmean(v41x_thermalckmin )
gen alckmin_therm5=tempalckmin-templulaheloisa if wave==5
gen alckmin_threshold5=(alckmin_therm5+10)/20

	*For Helena voters
egen templulaalckmin=rowmean(v41a_thermlula v41x_thermalckmin )
egen temphelena=rowmean(v41y_thermheloisa)
gen helena_therm5=temphelena-templulaalckmin if wave==5
gen helena_threshold5=(helena_therm5+10)/20

	*Lula Pres Approval
recode v81_lulagovpresapprove 1=5 2=4 3=3 4=2 5=1 8=. 9=. 99=.
 
*PARTISAN INTENSITY

recode v43b_pidparty (4=1) (else=0), gen(partyintensePT)
replace partyintensePT=.5 if v44b_pidparty==4
replace partyintensePT=. if respond==0

recode v43b_pidparty (3=1) (else=0), gen(partyintensePSDB)
replace partyintensePSDB=.5 if v44b_pidparty==3
replace partyintensePSDB=. if respond==0

recode v43b_pidparty (13=1) (else=0), gen(partyintensePPS)
replace partyintensePPS=.5 if v44b_pidparty==13
replace partyintensePPS=. if respond==0

recode v43b_pidparty (14=1) (else=0), gen(partyintensePSB)
replace partyintensePSB=.5 if v44b_pidparty==14
replace partyintensePSB=. if respond==0

recode v43b_pidparty (22=1) (else=0), gen(partyintensePSOL)
replace partyintensePSOL=.5 if v44b_pidparty==22
replace partyintensePSOL=. if respond==0

recode v43b_pidparty (1=1) (else=0), gen(partyintensePMDB)
replace partyintensePMDB=.5 if v44b_pidparty==1
replace partyintensePMDB=. if respond==0

gen partyintense_baseline=0
replace partyintense_baseline=1 if partyintensePT==0 & partyintensePSDB==0
replace partyintense_baseline=.5 if v44b_pidparty==2 | v44b_pidparty==1
replace partyintense_baseline=.5 if v44b_pidparty>4 & v44b_pidparty<20
replace partyintense_baseline=0 if v44b_pidparty==3
replace partyintense_baseline=0 if v44b_pidparty==4
replace partyintense_baseline=. if wave==2 | wave==3 | wave==4 | wave==6 
replace partyintense_baseline=. if respond~=1

*egen partyintenseSerra=rowmax(partyintensePSDB partyintensePMDB)

*ISSUE DIMENSION
mvdecode v63a_issuespriv v64_issuesfreetrade v65_issuesalca v70a_issueslandreform v68_issuesdeathpenalty v69a_issuessocialspend, mv(8)
mvdecode v63a_issuespriv v64_issuesfreetrade v65_issuesalca v70a_issueslandreform v68_issuesdeathpenalty v69a_issuessocialspend, mv(9)
mvdecode v40k_issuesthermpriv v40l_issuesthermfreetrade v40m_issuesthermdeathpenalty v40n_issuesthermlandreform v40r_issuesthermalca v40s_issuesthermbolsa, mv(18)
mvdecode v40k_issuesthermpriv v40l_issuesthermfreetrade v40m_issuesthermdeathpenalty v40n_issuesthermlandreform v40r_issuesthermalca v40s_issuesthermbolsa , mv(19)

	*wave 1
factor v63a_issuespriv  v64_issuesfreetrade v70a_issueslandreform v68_issuesdeathpenalty  if wave==1, pcf factors(1)
predict tempissues1
impute tempissues1  v63a_issuespriv v64_issuesfreetrade v70a_issueslandreform v68_issuesdeathpenalty  if respond==1 & wave==1, gen(issues)

	*wave 5
factor v63a_issuespriv v70a_issueslandreform v40k_issuesthermpriv v40l_issuesthermfreetrade v40r_issuesthermalca  if wave==5, pcf factors(1)
predict tempissues5
impute tempissues5  v63a_issuespriv v70a_issueslandreform v40k_issuesthermpriv v40l_issuesthermfreetrade v40r_issuesthermalca  if respond==1 & wave==5, gen(issues5)
replace issues5=-issues5

sort id 
merge m:1 id using "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Data\awarez.dta"
replace awarez=. if wave~=1
*replace awarez5=. if wave~=5


*GEN AWARENESS FOR COMPARISON TO DISCUSSANTS
	*MR
gen correct2d=0  if respond==1
replace correct2d=1 if s14b_know2vicepres==2
gen correct3d=0  if respond==1
replace correct3d=1 if s14c_know3fhcparty==3
gen correct4d=0  if respond==1
replace correct4d=1 if s14d_know4mercosul==2

*Awareness index is based on results of this IRT model 
/*
	irt 2pl correct2d correct3d correct4d if respond==1
	predict awaretemp, latent ebmean
	egen awaretempz=std(awaretemp)
	reg awaretempz correct2d correct3d correct4d
*/

gen aware_comparetodisc=correct2d*1.32083+correct3d*.90794832 +correct4d* .882677-1.755655 
replace aware_comparetodisc=. if wave==2 | wave>3
sort id wave
gen l2aware_comparetodisc=l2.aware_comparetodisc
	*note rather minimal learning of these answers between waves 1 and waves 3
ttest l2aware_comparetodisc==aware_comparetodisc if wave==3

	*D1
gen d_respond1=0
replace d_respond1=1 if d1_date~=.
gen d1_correct2d=0 if d_respond1==1
replace d1_correct2d=1 if d1s14b_know2vicepres==2
gen d1_correct3d=0  if d_respond1==1
replace d1_correct3d=1 if d1s14c_know3fhcparty==3
gen d1_correct4d=0  if d_respond1==1
replace d1_correct4d=1 if d1s14d_know4mercosul==2
gen d_aware_comparetodisc1=d1_correct2d*1.32083+d1_correct3d*.90794832 +d1_correct4d*.882677-1.755655

	*D2
gen d_respond2=0
replace d_respond2=1 if d2_date~=.
gen d2_correct2d=0 if d_respond2==1
replace d2_correct2d=1 if d2s14b_know2vicepres==2
gen d2_correct3d=0  if d_respond2==1
replace d2_correct3d=1 if d2s14c_know3fhcparty==3
gen d2_correct4d=0  if d_respond2==1
replace d2_correct4d=1 if d2s14d_know4mercosul==2
gen d_aware_comparetodisc2=d2_correct2d*1.32083+d2_correct3d*.90794832 +d2_correct4d*.882677-1.755655

gen awarenessdiffd_d1= d_aware_comparetodisc1- d_aware_comparetodisc2
gen awarenessdiffd_d2= d_aware_comparetodisc2- d_aware_comparetodisc1

reshape long discnamed discrelate discpresvote cs_discgovvote jf_discgovvote d_aware_comparetodisc awarenessdiffd_d, i(id wave) j(discid) 
sort id discid wave

gen newid=id*10+discid
drop if newid==.
xtset newid wave

*IMPORTANT
		*imputing discussant-validated vote:  
			*comment out these six rows to use perceptions of discussant vote. 
			*uncomment out to use actual discussant vote when available	

replace discpresvote=d1v22c_presvote if d1_orderinmainrespondentlist==1 & discid==1 & d1v22c_presvote~=. & wave==3
replace discpresvote=d1v22c_presvote if d1_orderinmainrespondentlist==2 & discid==2 & d1v22c_presvote~=. & wave==3
replace discpresvote=d1v22c_presvote if d1_orderinmainrespondentlist==3 & discid==3 & d1v22c_presvote~=. & wave==3

replace discpresvote=d2v22c_presvote if d2_orderinmainrespondentlist==1 & discid==1 & d2v22c_presvote~=. & wave==3
replace discpresvote=d2v22c_presvote if d2_orderinmainrespondentlist==2 & discid==2 & d2v22c_presvote~=. & wave==3
replace discpresvote=d2v22c_presvote if d2_orderinmainrespondentlist==3 & discid==3 & d2v22c_presvote~=. & wave==3
*/

	*Discussant relationship
recode discrelate 1/3=1 4/6=0 else=., gen(discfamily)

	*others coded as 9s in waves 5 and 6.  this fixes that.
recode discpresvote 9=16 if wave==5 | wave==6

recode discpresvote 1=1 2=2 3=16 4=4 5=5 6=16 7=16 8=16 9=16 10=10 11=16 12=12 13=16 14=16 16=16 17=17 18=17 19=17 99=17
replace discpresvote=. if discnamed==0

recode v22c_presvote 1=1 2=2 3=16 4=4 5=5 6=16 7=16 8=16 9=16 10=10 11=16 12=12 13=16 14=16 16=16 17=17 18=17 19=17, gen(presvote)

*Recoding Others to missing because it creates a problem for some categories. Can toggle this on and off
recode discpresvote 16=. if wave==2 | wave==3
recode presvote 16=. if wave==2
recode presvote 16=. if wave==3

*recode discpresvote 16=. if wave==5 
*recode discpresvote 16=. if wave==6 

label define presvotetemp 1 Ciro 2 Lula 3 Roseana 4 Serra 5 Garotinho  6 Itamar 7 "Jose Maria" 8 "Rui Costa Pimenta"  9 "Fernando Henrique Cardoso" 10 "Alckmin" 11 "Buarque" 12 "Helena" 13 "Eymael" 14 "Bivar" 16 Other 17 "No One" 18 DK 19 NR
label values presvote presvotetemp
label values discpresvote presvotetemp

*PARTY CONTACT
recode v38b1b_persuade1frompartyname v38b2b_persuade2frompartyname v38b3b_persuade3frompartyname (4=2) (12=2) (8=2) (9=2) (3=4) (1=4) (2=4) (5=4) (14=5) (13=1) (6=1) (7=1) (10=16) (11=16) (20=16) (21=16) (else=0), gen(partycontact1 partycontact2 partycontact3)
egen lulacontact=anycount(partycontact1 partycontact2 partycontact3), values(2)
egen serracontact=anycount(partycontact1 partycontact2 partycontact3), values(4)
egen cirocontact=anycount(partycontact1 partycontact2 partycontact3), values(1)
egen agcontact=anycount(partycontact1 partycontact2 partycontact3), values(5)
replace lulacontact=. if respond~=1 | wave~=3
replace serracontact=. if respond~=1  | wave~=3
replace cirocontact=. if respond~=1  | wave~=3
replace agcontact=. if respond~=1  | wave~=3
 
recode v38b1b_persuade1frompartyname v38b2b_persuade2frompartyname v38b3b_persuade3frompartyname (1=2) (4=2) (5=2) (8=2) (12=2) (14=2) (2=10) (3=10) (13=10) (6=16) (10=12) (7=16) (9=16) (11=16) (20=16) (21=16) (else=0), gen(partycontact061 partycontact062 partycontact063)
egen lulacontact06=anycount(partycontact061 partycontact062 partycontact063), values(2)
egen alckmincontact=anycount(partycontact061 partycontact062 partycontact063), values(10)
egen helenacontact=anycount(partycontact061 partycontact062 partycontact063), values(12)
replace lulacontact06=. if respond~=1 | wave~=6
replace alckmincontact=. if respond~=1  | wave~=6
replace helenacontact=. if respond~=1  | wave~=6

*Awareness
gen awarenessdiff=d_aware_comparetodisc-aware_comparetodisc

*Analysis 
	

constraint drop _all
constraint define 1 [Ciro]5L.presvote==
constraint define 2 [Serra]5L.presvote==

*2002
recode presvote 1=4 2=1 4=2 5=3 17=17, gen(presvote_dv)
recode discpresvote 1=4 2=1 4=2 5=3 17=17, gen(discpresvote_dv)
label define vote_dv 1 Lula 2 Serra 3 Garotinho 4 Ciro 10 Alckmin 12 Helena 16 Other 17 "No One" 
label values presvote_dv vote_dv
label values discpresvote_dv vote_dv

	*Quick analysis of second round vote
recode v22d_presvote2ndtrnlulavserra 2=0 1=1 3=. 8=. 9=., gen(presvote_rnd2_dv)
		*main model
*logit presvote_rnd2_dv ib2.discpresvote_dv ib2.l.discpresvote_dv l2.v5d_econpastbrazil l2.partyintensePT l2.partyintense_baseline l2.issues ib2.l.presvote_dv ib2.l2.presvote_dv ///
*city if wave==3 & presvote_dv~=1 & presvote_dv~=2  & presvote_rnd2_dv<16, cluster(id) 

eststo clear
eststo: logit presvote_rnd2_dv ib2.discpresvote_dv ib2.l.discpresvote_dv l2.v5d_econpastbrazil l2.partyintensePT l2.partyintense_baseline l2.issues ib2.l.presvote_dv ib2.l2.presvote_dv ///
city if wave==3 & (presvote_dv==3| presvote_dv==4|presvote_dv==16)	 & presvote_rnd2_dv<16, cluster(id) 
cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
esttab est1 using Brazil2002dyadic2ndround.rtf, label b(3) replace se star(* 0.05 ) nogap onecell
eststo clear

tab presvote_dv, gen(presvote_dv)
tab discpresvote_dv, gen(discpresvote_dv)

*Table A.10
logit presvote_rnd2_dv discpresvote_dv1 discpresvote_dv3 discpresvote_dv4 discpresvote_dv8 l.discpresvote_dv1 l.discpresvote_dv3 l.discpresvote_dv4 l.discpresvote_dv8 ///
l2.v5d_econpastbrazil l2.partyintensePT l2.partyintense_baseline l2.issues l.presvote_dv1 l.presvote_dv3 l.presvote_dv4 l.presvote_dv8 l2.presvote_dv1 l2.presvote_dv3 l2.presvote_dv4 l2.presvote_dv7 l2.presvote_dv8   ///
city if wave==3 & (presvote_dv==3| presvote_dv==4|presvote_dv==16)	 & presvote_rnd2_dv<16, cluster(id) 

*Figure 4.7
	*effect of lula versus serra voter when mr is garo voter
margins, at(discpresvote_dv1==0 discpresvote_dv3==0 discpresvote_dv4==0 discpresvote_dv8==0 l.discpresvote_dv1==0 l.discpresvote_dv3==0 l.discpresvote_dv4==0 l.discpresvote_dv8==0 ///
l.presvote_dv1==0 l.presvote_dv3==1 l.presvote_dv4==0 l.presvote_dv8==0 l2.presvote_dv1==0 l2.presvote_dv3==1 l2.presvote_dv4==0 l2.presvote_dv7==0 l2.presvote_dv8==0) /// 
at(discpresvote_dv1==1 discpresvote_dv3==0 discpresvote_dv4==0 discpresvote_dv8==0 l.discpresvote_dv1==1 l.discpresvote_dv3==0 l.discpresvote_dv4==0 l.discpresvote_dv8==0 ///
l.presvote_dv1==0 l.presvote_dv3==1 l.presvote_dv4==0 l.presvote_dv8==0 l2.presvote_dv1==0 l2.presvote_dv3==1 l2.presvote_dv4==0 l2.presvote_dv7==0 l2.presvote_dv8==0) atmeans contrast(atcontrast(r))

margins, at(discpresvote_dv1==0 discpresvote_dv3==0 discpresvote_dv4==0 discpresvote_dv8==0 ///
l.presvote_dv1==0 l.presvote_dv3==1 l.presvote_dv4==0 l.presvote_dv8==0 l2.presvote_dv1==0 l2.presvote_dv3==1 l2.presvote_dv4==0 l2.presvote_dv7==0 l2.presvote_dv8==0) /// 
at(discpresvote_dv1==1 discpresvote_dv3==0 discpresvote_dv4==0 discpresvote_dv8==0 ///
l.presvote_dv1==0 l.presvote_dv3==1 l.presvote_dv4==0 l.presvote_dv8==0 l2.presvote_dv1==0 l2.presvote_dv3==1 l2.presvote_dv4==0 l2.presvote_dv7==0 l2.presvote_dv8==0) atmeans contrast(atcontrast(r))

	*effect of lula versus serra voter when mr is ciro voter
margins, at(discpresvote_dv1==0 discpresvote_dv3==0 discpresvote_dv4==0 discpresvote_dv8==0 l.discpresvote_dv1==0 l.discpresvote_dv3==0 l.discpresvote_dv4==0 l.discpresvote_dv8==0 ///
l.presvote_dv1==0 l.presvote_dv3==0 l.presvote_dv4==1 l.presvote_dv8==0 l2.presvote_dv1==0 l2.presvote_dv3==1 l2.presvote_dv4==0 l2.presvote_dv7==0 l2.presvote_dv8==0) /// 
at(discpresvote_dv1==1 discpresvote_dv3==0 discpresvote_dv4==0 discpresvote_dv8==0 l.discpresvote_dv1==1 l.discpresvote_dv3==0 l.discpresvote_dv4==0 l.discpresvote_dv8==0 ///
l.presvote_dv1==0 l.presvote_dv3==0 l.presvote_dv4==1 l.presvote_dv8==0 l2.presvote_dv1==0 l2.presvote_dv3==1 l2.presvote_dv4==0 l2.presvote_dv7==0 l2.presvote_dv8==0) atmeans contrast(atcontrast(r))

margins, at(discpresvote_dv1==0 discpresvote_dv3==0 discpresvote_dv4==0 discpresvote_dv8==0 ///
l.presvote_dv1==0 l.presvote_dv3==0 l.presvote_dv4==1 l.presvote_dv8==0 l2.presvote_dv1==0 l2.presvote_dv3==1 l2.presvote_dv4==0 l2.presvote_dv7==0 l2.presvote_dv8==0) /// 
at(discpresvote_dv1==1 discpresvote_dv3==0 discpresvote_dv4==0 discpresvote_dv8==0 ///
l.presvote_dv1==0 l.presvote_dv3==0 l.presvote_dv4==1 l.presvote_dv8==0 l2.presvote_dv1==0 l2.presvote_dv3==1 l2.presvote_dv4==0 l2.presvote_dv7==0 l2.presvote_dv8==0) atmeans contrast(atcontrast(r))

		*1: Full model
*Table A.6
eststo clear
eststo: mlogit presvote_dv ib2.discpresvote_dv ib2.l.discpresvote_dv l2.v5d_econpastbrazil l2.partyintensePT l2.partyintense_baseline l2.issues ib2.l.presvote_dv ib2.l2.presvote_dv ///
city if wave==3 & presvote_dv<16, cluster(id) base(2) 

mat define eb= get(_b)
mat define eV=get(VCE)
mat B1=eb["y1","Lula:1.discpresvote_dv".."Lula:17.discpresvote_dv"]
mat B2=eb["y1","Garotinho:1.discpresvote_dv".."Garotinho:17.discpresvote_dv"]
mat B3=eb["y1","Ciro:1.discpresvote_dv".."Ciro:17.discpresvote_dv"]
mat V1a=eV["Lula:1.discpresvote_dv","Lula:1.discpresvote_dv"]
mat V2a=eV["Lula:2b.discpresvote_dv","Lula:2b.discpresvote_dv"]
mat V3a=eV["Lula:3.discpresvote_dv","Lula:3.discpresvote_dv"]
mat V4a=eV["Lula:4.discpresvote_dv","Lula:4.discpresvote_dv"]
mat V5a=eV["Lula:17.discpresvote_dv","Lula:17.discpresvote_dv"]
mat V1b=eV["Garotinho:1.discpresvote_dv","Garotinho:1.discpresvote_dv"]
mat V2b=eV["Garotinho:2.discpresvote_dv","Garotinho:2b.discpresvote_dv"]
mat V3b=eV["Garotinho:3.discpresvote_dv","Garotinho:3.discpresvote_dv"]
mat V4b=eV["Garotinho:4.discpresvote_dv","Garotinho:4.discpresvote_dv"]
mat V5b=eV["Garotinho:17.discpresvote_dv","Garotinho:17.discpresvote_dv"]
mat V1c=eV["Ciro:1.discpresvote_dv","Ciro:1.discpresvote_dv"]
mat V2c=eV["Ciro:2b.discpresvote_dv","Ciro:2b.discpresvote_dv"]
mat V3c=eV["Ciro:3.discpresvote_dv","Ciro:3.discpresvote_dv"]
mat V4c=eV["Ciro:4.discpresvote_dv","Ciro:4.discpresvote_dv"]
mat V5c=eV["Ciro:17.discpresvote_dv","Ciro:17.discpresvote_dv"]
mat B=[.,B1,.,B2,.,B3]
mat V=[.,V1a,V2a,V3a,V4a,V5a,.,V1b,V2b,V3b,V4b,V5b,.,V1c,V2c,V3c,V4c,V5c]
mat BFull=[B]'
mat VFull=[V]'
svmat BFull 
svmat VFull 

gen n=_n*-1

gen upperFull=BFull+1.9645*(VFull^.5)
gen lowerFull=BFull-1.9645*(VFull^.5)

*Figure 4.5A
twoway (rcap upperFull lowerFull n if n>-19, color(black) horizontal ) (scatter n BFull if n>-19, mcolor(black)) , ///
xtitle("{bf:Multinomial Logit Coefficient}", size(medium)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("") ///
legend(off) xline(0, lcolor(black)) yline(-12.5 -6.5, lcolor(black))  ///
ylab(-2 "{bf:Alter vote choice=Lula}" -3 "Alter vote choice=Serra" -4 "Alter vote choice=Garo." -5 "Alter vote choice=Ciro" -6 "Alter vote choice=none" ///
-8 "Alter vote choice=Lula" -9 "Alter vote choice=Serra" -10 "{bf:Alter vote choice=Garo.}" -11 "Alter vote choice=Ciro" -12 "Alter vote choice=none"  ///
-14 "Alter vote choice=Lula" -15 "Alter vote choice=Serra" -16 "Alter vote choice=Garo." -17 "{bf:Alter vote choice=Ciro}" -18 "Alter vote choice=none"  , angle(horizontal) notick )  yscale(range(-15 -1)) xscale(range(0 3)) ///
xlab(0 .5 1 1.5 2 2.5 3 3.5) ytick(-12.5 -6.5, tlength(40) tlcolor(black)) ytitle("{bf:Independent Variables}", size(medium)) text(-1 2 "{bf:Pr(Y{it:{sub:e}}=Lula) {&frasl} Pr(Y{it:{sub:e}}=Serra)}", box bcolor(white)) ///
text(-7 2 "{bf:Pr(Y{it:{sub:e}}=Garo.) {&frasl} Pr(Y{it:{sub:e}}=Serra)}", box bcolor(white)) text(-13 2 "{bf:Pr(Y{it:{sub:e}}=Ciro) {&frasl} Pr(Y{it:{sub:e}}=Serra)}", box bcolor(white)) note("{it:N}=5,127", span size(medsmall))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2002DyadicFull.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_5A.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_5A.pdf", as(pdf) replace 

*	Just to get predicted prob, needed for relative risk ratios
tab discpresvote, gen(discpresvote)
	
mlogit presvote_dv discpresvote1 discpresvote2 discpresvote4 discpresvote8 l.discpresvote1 l.discpresvote2 l.discpresvote4 l.discpresvote8 ///
l2.v5d_econpastbrazil l2.partyintensePT l2.partyintense_baseline l2.issues l.presvote_dv1 l.presvote_dv3 l.presvote_dv4 l.presvote_dv8 l2.presvote_dv1 l2.presvote_dv3 l2.presvote_dv4 l2.presvote_dv7 l2.presvote_dv8 city  /// 
if wave==3 & presvote_dv<16, cluster(id) base(2)  

	*Serra to Serra
margins, at(discpresvote1==0 discpresvote2==0 discpresvote4==0 discpresvote8==0 l.discpresvote1==0 l.discpresvote2==0 l.discpresvote4==0 l.discpresvote8==0) atmeans predict(outcome(1)) predict(outcome(2)) predict(outcome(3)) predict(outcome(4))
	*Serra to Lula
margins, at(discpresvote1==0 discpresvote2==1 discpresvote4==0 discpresvote8==0 l.discpresvote1==0 l.discpresvote2==0 l.discpresvote4==0 l.discpresvote8==0) atmeans predict(outcome(1)) predict(outcome(2)) predict(outcome(3)) predict(outcome(4))

	*Gomes to Gomes
margins, at(discpresvote1==1 discpresvote2==0 discpresvote4==0 discpresvote8==0 l.discpresvote1==1 l.discpresvote2==0 l.discpresvote4==0 l.discpresvote8==0) atmeans predict(outcome(1)) predict(outcome(2)) predict(outcome(3)) predict(outcome(4))
	*Gomes to Serra
margins, at(discpresvote1==0 discpresvote2==0 discpresvote4==0 discpresvote8==0 l.discpresvote1==1 l.discpresvote2==0 l.discpresvote4==0 l.discpresvote8==0) atmeans predict(outcome(1)) predict(outcome(2)) predict(outcome(3)) predict(outcome(4))

		*2 and 3a: Stable Discussants
*Table A.6
eststo: mlogit presvote_dv ib2.discpresvote_dv l2.v5d_econpastbrazil l2.partyintensePT l2.partyintense_baseline l2.issues ib2.l.presvote_dv ib2.l2.presvote_dv ///
city if wave==3 & presvote_dv<16 & discpresvote_dv==l.discpresvote_dv, cluster(id) base(2) 

mat define eb= get(_b)
mat define eV=get(VCE)
mat B1=eb["y1","Lula:1.discpresvote_dv".."Lula:17.discpresvote_dv"]
mat B2=eb["y1","Garotinho:1.discpresvote_dv".."Garotinho:17.discpresvote_dv"]
mat B3=eb["y1","Ciro:1.discpresvote_dv".."Ciro:17.discpresvote_dv"]
mat V1a=eV["Lula:1.discpresvote_dv","Lula:1.discpresvote_dv"]
mat V2a=eV["Lula:2b.discpresvote_dv","Lula:2b.discpresvote_dv"]
mat V3a=eV["Lula:3.discpresvote_dv","Lula:3.discpresvote_dv"]
mat V4a=eV["Lula:4.discpresvote_dv","Lula:4.discpresvote_dv"]
mat V5a=eV["Lula:17.discpresvote_dv","Lula:17.discpresvote_dv"]
mat V1b=eV["Garotinho:1.discpresvote_dv","Garotinho:1.discpresvote_dv"]
mat V2b=eV["Garotinho:2b.discpresvote_dv","Garotinho:2b.discpresvote_dv"]
mat V3b=eV["Garotinho:3.discpresvote_dv","Garotinho:3.discpresvote_dv"]
mat V4b=eV["Garotinho:4.discpresvote_dv","Garotinho:4.discpresvote_dv"]
mat V5b=eV["Garotinho:17.discpresvote_dv","Garotinho:17.discpresvote_dv"]
mat V1c=eV["Ciro:1.discpresvote_dv","Ciro:1.discpresvote_dv"]
mat V2c=eV["Ciro:2b.discpresvote_dv","Ciro:2b.discpresvote_dv"]
mat V3c=eV["Ciro:3.discpresvote_dv","Ciro:3.discpresvote_dv"]
mat V4c=eV["Ciro:4.discpresvote_dv","Ciro:4.discpresvote_dv"]
mat V5c=eV["Ciro:17.discpresvote_dv","Ciro:17.discpresvote_dv"]
mat B=[.,B1,.,B2,.,B3]
mat V=[.,V1a,V2a,V3a,V4a,V5a,.,V1b,V2b,V3b,V4b,V5b,.,V1c,V2c,V3c,V4c,V5c]
mat BStableDisc=[B]'
mat VStableDisc=[V]'
svmat BStableDisc 
svmat VStableDisc

gen upperStableDisc=BStableDisc+1.9645*(VStableDisc^.5)
gen lowerStableDisc=BStableDisc-1.9645*(VStableDisc^.5)

*Figure 4.6a
twoway (rcap upperStableDisc lowerStableDisc n if n>-19, color(black) horizontal ) (scatter n BStableDisc if n>-19, mcolor(black)) , ///
xtitle("{bf:Multinomial Logit Coefficient}", size(medium)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("") ///
legend(off) xline(0, lcolor(black)) yline(-12.5 -6.5, lcolor(black))  ///
ylab(-2 "{bf:Alter vote intention=Lula}" -3 "Alter vote intention=Serra" -4 "Alter vote intention=Garo." -5 "Alter vote intention=Ciro" -6 "Alter vote intention=none" ///
-8 "Alter vote intention=Lula" -9 "Alter vote intention=Serra" -10 "{bf:Alter vote intention=Garo.}" -11 "Alter vote intention=Ciro" -12 "Alter vote intention=none"  ///
-14 "Alter vote intention=Lula" -15 "Alter vote intention=Serra" -16 "Alter vote intention=Garo." -17 "{bf:Alter vote intention=Ciro}" -18 "Alter vote intention=none"  , angle(horizontal) notick )  yscale(range(-15 -1)) xscale(range(0 3)) ///
xlab(-.5 0 .5 1 1.5 2 2.5 3 3.5 4 4.5 5) ytick(-12.5 -6.5, tlength(44) tlcolor(black)) ytitle("{bf:Independent Variables}", size(medium)) text(-1 2 "{bf:Pr(Y{it:{sub:e}}=Lula) {&frasl} Pr(Y{it:{sub:e}}=Serra)}", box bcolor(white)) ///
text(-7 2 "{bf:Pr(Y{it:{sub:e}}=Garotinho) {&frasl} Pr(Y{it:{sub:e}}=Serra)}", box bcolor(white)) text(-13 2 "{bf:Pr(Y{it:{sub:e}}=Ciro) {&frasl} Pr(Y{it:{sub:e}}=Serra)}", box bcolor(white)) note("{it:N}=2,678", span size(medsmall))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2002DyadicStable.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_6A.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_6A.pdf", as(pdf) replace 

mat B1=eb["y1","Lula:1.discpresvote_dv"]
mat B2=eb["y1","Serra:2b.discpresvote_dv"]
mat B3=eb["y1","Garotinho:3.discpresvote_dv"]
mat B4=eb["y1","Ciro:4.discpresvote_dv"]

mat B=[B1,B3,B4,B2]
mat V=[V1a,V3b,V4c,0]
mat BStableDisc2=[B]'
mat VStableDisc2=[V]'
svmat BStableDisc2
svmat VStableDisc2

gen upperStableDisc2=BStableDisc2+1.9645*(VStableDisc2^.5)
gen lowerStableDisc2=BStableDisc2-1.9645*(VStableDisc2^.5)

*	Just to get predicted prob, needed for relative risk ratios
	
mlogit presvote_dv discpresvote1 discpresvote2 discpresvote4 discpresvote8 l.discpresvote1 l.discpresvote2 l.discpresvote4 l.discpresvote8 ///
l2.v5d_econpastbrazil l2.partyintensePT l2.partyintense_baseline l2.issues l.presvote_dv1 l.presvote_dv3 l.presvote_dv4 l.presvote_dv8 l2.presvote_dv1 l2.presvote_dv3 l2.presvote_dv4 l2.presvote_dv7 l2.presvote_dv8 city  /// 
if wave==3 & presvote_dv<16 & discpresvote_dv==l.discpresvote_dv, cluster(id) base(2)  

	*Serra to Serra
margins, at(discpresvote1==0 discpresvote2==0 discpresvote4==0 discpresvote8==0 l.discpresvote1==0 l.discpresvote2==0 l.discpresvote4==0 l.discpresvote8==0) atmeans predict(outcome(1)) predict(outcome(2)) predict(outcome(3)) predict(outcome(4))

*-------------------
recode presvote_dv 2=0 1=1 else=., gen(serra_lula)
recode presvote_dv 2=0 3=1 else=., gen(serra_garo)
recode presvote_dv 2=0 4=1 else=., gen(serra_ciro)

*Instrumental Vars
ivregress 2sls  serra_lula l2.v5d_econpastbrazil l2.partyintensePT l2.partyintense_baseline l2.issues ib2.l.presvote_dv ib2.l2.presvote_dv ///
city (ib2.discpresvote_dv=ib2.l.discpresvote_dv) if wave==3, cluster(id) first

ivregress 2sls  serra_garo l2.v5d_econpastbrazil l2.partyintensePT l2.partyintense_baseline l2.issues ib2.l.presvote_dv ib2.l2.presvote_dv ///
city (ib2.discpresvote_dv=ib2.l.discpresvote_dv) if wave==3 , cluster(id)  

ivregress 2sls  serra_ciro l2.v5d_econpastbrazil l2.partyintensePT l2.partyintense_baseline l2.issues ib2.l.presvote_dv ib2.l2.presvote_dv ///
city (ib2.discpresvote_dv=ib2.l.discpresvote_dv) if wave==3 , cluster(id)  first

*Friends only, for environmental confounding
eststo clear
eststo: mlogit presvote_dv ib2.discpresvote_dv ib2.l.discpresvote_dv l2.v5d_econpastbrazil l2.partyintensePT l2.partyintense_baseline l2.issues ib2.l.presvote_dv ib2.l2.presvote_dv  ///
city if wave==3 & presvote_dv<16 & l.discfamily==0, cluster(id) base(2) 

eststo: mlogit presvote_dv ib2.discpresvote_dv l2.v5d_econpastbrazil l2.partyintensePT l2.partyintense_baseline l2.issues ib2.l.presvote_dv ib2.l2.presvote_dv ///
city if wave==3 & presvote_dv<16 & discpresvote_dv==l.discpresvote_dv & l.discfamily==0, cluster(id) base(2) 

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
esttab est1 est2 using Brazil2002dyadicFRIENDS.rtf, label b(3) replace se star(* 0.05 ) nogap onecell
eststo clear


*2006
*Listwise deletion
constraint drop _all
constraint define 1 [Alckmin]12L.discpresvote#cL5.awarez==0
constraint define 2 [Alckmin]17L.discpresvote#cL5.awarez==0
constraint define 3 [Helena]10L.discpresvote#cL5.awarez==0
constraint define 4 [Helena]17L.discpresvote#cL5.awarez==0

*2006
	*Quick analysis of second round vote
recode v22i_presvote2ndturnlulavalck 2=1 1=0 3=. 8=. 9=., gen(presvote_rnd2_06_dv)
		*main model
*logit presvote_rnd2_06_dv ib1.discpresvote_dv ib1.l.discpresvote_dv l1.v5d_econpastbrazil l1.v81_lulagovpresapprove l1.partyintensePSDB l1.partyintense_baseline l.issues5 ib1.l.presvote_dv ///
*city if wave==6 & presvote_dv~=1 & presvote_dv~=10 , cluster(id) 

eststo clear
eststo: logit presvote_rnd2_06_dv ib1.discpresvote_dv ib1.l.discpresvote_dv l1.v5d_econpastbrazil l1.v81_lulagovpresapprove l1.partyintensePSDB l1.partyintense_baseline l.issues5 ib1.l.presvote_dv ///
city if wave==6 & (presvote_dv==12 | presvote_dv==16) , cluster(id) 
cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
esttab est1 using Brazil2006dyadic2ndround.rtf, label b(3) replace se star(* 0.05 ) nogap onecell
eststo clear

*Table A.10
logit presvote_rnd2_06_dv discpresvote_dv5 discpresvote_dv6 discpresvote_dv7 discpresvote_dv8 l.discpresvote_dv5 l.discpresvote_dv6 l.discpresvote_dv7 l.discpresvote_dv8 ///
 l1.v5d_econpastbrazil l1.v81_lulagovpresapprove l1.partyintensePSDB l1.partyintense_baseline l.issues5 l.presvote_dv5 l.presvote_dv6 l.presvote_dv7 l.presvote_dv8  ///
city if wave==6 & (presvote_dv==12 | presvote_dv==16) , cluster(id) 
xyz
	*effect of lula versus alckmin voter when mr is helena voter
*Figure 4.7
margins, at(discpresvote_dv5==0 discpresvote_dv6==0 discpresvote_dv7==0 discpresvote_dv8==0 ///
l.discpresvote_dv5==0 l.discpresvote_dv6==0 l.discpresvote_dv7==0 l.discpresvote_dv8==0 ///
l.presvote_dv5==0 l.presvote_dv6==1 l.presvote_dv7==0 l.presvote_dv8==0) ///
at(discpresvote_dv5==1 discpresvote_dv6==0 discpresvote_dv7==0 discpresvote_dv8==0 ///
l.discpresvote_dv5==1 l.discpresvote_dv6==0 l.discpresvote_dv7==0 l.discpresvote_dv8==0 ///
l.presvote_dv5==0 l.presvote_dv6==1 l.presvote_dv7==0 l.presvote_dv8==0) atmeans contrast(atcontrast(r))

margins, at(discpresvote_dv5==0 discpresvote_dv6==0 discpresvote_dv7==0 discpresvote_dv8==0 ///
l.presvote_dv5==0 l.presvote_dv6==1 l.presvote_dv7==0 l.presvote_dv8==0) ///
at(discpresvote_dv5==1 discpresvote_dv6==0 discpresvote_dv7==0 discpresvote_dv8==0 ///
l.presvote_dv5==0 l.presvote_dv6==1 l.presvote_dv7==0 l.presvote_dv8==0) atmeans contrast(atcontrast(r))

		*Full model
*Table A.7
eststo clear
eststo: mlogit presvote_dv ib1.discpresvote_dv ib1.l.discpresvote_dv l1.v5d_econpastbrazil l1.v81_lulagovpresapprove l1.partyintensePSDB l1.partyintense_baseline l.issues5 ib1.l.presvote_dv ///
city if wave==6 & presvote_dv<16, cluster(id) base(1) 

mat define eb= get(_b)
mat define eV=get(VCE)
mat B1=eb["y1","Alckmin:1b.discpresvote_dv".."Alckmin:17.discpresvote_dv"]
mat B2=eb["y1","Helena:1b.discpresvote_dv".."Helena:17.discpresvote_dv"]
mat V1a=eV["Alckmin:1b.discpresvote_dv","Alckmin:1b.discpresvote_dv"]
mat V2a=eV["Alckmin:10.discpresvote_dv","Alckmin:10.discpresvote_dv"]
mat V3a=eV["Alckmin:12.discpresvote_dv","Alckmin:12.discpresvote_dv"]
mat V4a=eV["Alckmin:16.discpresvote_dv","Alckmin:16.discpresvote_dv"]
mat V5a=eV["Alckmin:17.discpresvote_dv","Alckmin:17.discpresvote_dv"]
mat V1b=eV["Helena:1b.discpresvote_dv","Helena:1b.discpresvote_dv"]
mat V2b=eV["Helena:10.discpresvote_dv","Helena:10.discpresvote_dv"]
mat V3b=eV["Helena:12.discpresvote_dv","Helena:12.discpresvote_dv"]
mat V4b=eV["Helena:16.discpresvote_dv","Helena:16.discpresvote_dv"]
mat V5b=eV["Helena:17.discpresvote_dv","Helena:17.discpresvote_dv"]
mat B=[.,B1,.,B2]
mat V=[.,V1a,V2a,V3a,V4a,V5a,.,V1b,V2b,V3b,V4b,V5b]
mat BFull06=[B]'
mat VFull06=[V]'
svmat BFull06 
svmat VFull06

gen n06=_n*-1

gen upperFull06=BFull06+1.9645*(VFull06^.5)
gen lowerFull06=BFull06-1.9645*(VFull06^.5)

*figure 4.5B
twoway (rcap upperFull06 lowerFull06 n06 if n06>-13, color(black) horizontal ) (scatter n06 BFull06 if n06>-13, mcolor(black)) , ///
xtitle("{bf:Multinomial Logit Coefficient}", size(medium)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("") ///
legend(off) xline(0, lcolor(black)) yline(-6.5, lcolor(black))  ///
ylab(-2 "Alter vote choice=Lula" -3 "{bf:Alter vote choice=Alckmin}" -4 "Alter vote choice=HH" -5 "Alter vote choice=other" -6 "Alter vote choice=none" ///
-8 "Alter vote choice=Lula" -9 "Alter vote choice=Alckmin" -10 "{bf:Alter vote choice=HH}" -11 "Alter vote choice=other" -12 "Alter vote choice=none" , ///
angle(horizontal) notick )  yscale(range(-13 -1)) xscale(range(0 3)) ///
xlab(0 .5 1 1.5 2 2.5 3 3.5) ytick(-6.5, tlength(44) tlcolor(black)) ytitle("{bf:Independent Variables}", size(medium)) text(-1 1.8 "{bf:Pr(Y{it:{sub:e}}=Alckmin) {&frasl} Pr(Y{it:{sub:e}}=Lula)}", box bcolor(white)) ///
text(-7 1.8 "{bf:Pr(Y{it:{sub:e}}=Heloísa Helena) {&frasl} Pr(Y{it:{sub:e}}=Lula)}", box bcolor(white)) note("{it:N}=2,577", span size(medsmall))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2006DyadicFull.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_5B.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_5B.pdf", as(pdf) replace 

*get predicted probs, RR ratios
mlogit presvote_dv discpresvote_dv5 discpresvote_dv6 discpresvote_dv7 discpresvote_dv8 l.discpresvote_dv5 l.discpresvote_dv6 l.discpresvote_dv7 l.discpresvote_dv8 l1.v5d_econpastbrazil l1.v81_lulagovpresapprove l1.partyintensePSDB l1.partyintense_baseline l.issues5 ///
l.presvote_dv5 l.presvote_dv6 l.presvote_dv7 l.presvote_dv8  city if wave==6 & presvote_dv<16, cluster(id) base(1) 

*Lula to Lula
margins, at(discpresvote_dv5=0 discpresvote_dv6=0 discpresvote_dv7=0 discpresvote_dv8=0 l.discpresvote_dv5=0 l.discpresvote_dv6=0 l.discpresvote_dv7=0 l.discpresvote_dv8=0) atmeans predict(outcome(1)) predict(outcome(10)) predict(outcome(12)) 

		*Stable Discussants
*Table A.7
eststo: mlogit presvote_dv ib1.discpresvote_dv l1.v5d_econpastbrazil l1.v81_lulagovpresapprove l1.partyintensePSDB l1.partyintense_baseline l.issues5 ib1.l.presvote_dv ///
city if wave==6 & presvote_dv<16 & discpresvote==l.discpresvote, cluster(id) base(1) 

mat define eb= get(_b)
mat define eV=get(VCE)
mat B1=eb["y1","Alckmin:1b.discpresvote_dv".."Alckmin:17.discpresvote_dv"]
mat B2=eb["y1","Helena:1b.discpresvote_dv".."Helena:17.discpresvote_dv"]
mat V1a=eV["Alckmin:1b.discpresvote_dv","Alckmin:1b.discpresvote_dv"]
mat V2a=eV["Alckmin:10.discpresvote_dv","Alckmin:10.discpresvote_dv"]
mat V3a=eV["Alckmin:12.discpresvote_dv","Alckmin:12.discpresvote_dv"]
mat V4a=eV["Alckmin:16.discpresvote_dv","Alckmin:16.discpresvote_dv"]
mat V5a=eV["Alckmin:17.discpresvote_dv","Alckmin:17.discpresvote_dv"]
mat V1b=eV["Helena:1b.discpresvote_dv","Helena:1b.discpresvote_dv"]
mat V2b=eV["Helena:10.discpresvote_dv","Helena:10.discpresvote_dv"]
mat V3b=eV["Helena:12.discpresvote_dv","Helena:12.discpresvote_dv"]
mat V4b=eV["Helena:16.discpresvote_dv","Helena:16.discpresvote_dv"]
mat V5b=eV["Helena:17.discpresvote_dv","Helena:17.discpresvote_dv"]
mat B=[.,B1,.,B2]
mat V=[.,V1a,V2a,V3a,V4a,V5a,.,V1b,V2b,V3b,.,V5b]
mat BStableDisc06=[B]'
mat VStableDisc06=[V]'
svmat BStableDisc06 
svmat VStableDisc06

gen upperStableDisc06=BStableDisc06+1.9645*(VStableDisc06^.5)
gen lowerStableDisc06=BStableDisc06-1.9645*(VStableDisc06^.5)

replace BStableDisc06 =. if VStableDisc06==. 

*Figure 4.6B
twoway (rcap upperStableDisc06 lowerStableDisc06 n06 if n06>-13, color(black) horizontal ) (scatter n06 BStableDisc06 if n06>-13, mcolor(black)) , ///
xtitle("{bf:Multinomial Logit Coefficient}", size(medium)) ///
graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) ytitle("") ///
legend(off) xline(0, lcolor(black)) yline(-6.5, lcolor(black))  ///
ylab(-2 "Alter vote intention=Lula" -3 "{bf:Alter vote intention=Alckmin}" -4 "Alter vote intention=HH" -5 "Alter vote intention=other" -6 "Alter vote intention=none" ///
-8 "Alter vote intention=Lula" -9 "Alter vote intention=Alckmin" -10 "{bf:Alter vote intention=HH}" -11 "Alter vote intention=other" -12 "Alter vote intention=none" , ///
angle(horizontal) notick )  yscale(range(-13 -1)) xscale(range(0 3)) ///
xlab(-.5 0 .5 1 1.5 2 2.5 3 3.5 4.0 ) ytick(-6.5, tlength(48) tlcolor(black)) ytitle("{bf:Independent Variables}", size(medium)) text(-1 1.75 "{bf:Pr(Y{it:{sub:e}}=Alckmin) {&frasl} Pr(Y{it:{sub:e}}=Lula)}") ///
text(-7 1.75 "{bf:Pr(Y{it:{sub:e}}=Heloísa Helena) {&frasl} Pr(Y{it:{sub:e}}=Lula)}", box bcolor(white)) note("{it:N}=1,336", span size(medsmall))

graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Bra2006DyadicStable.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_6B.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_6B.pdf", as(pdf) replace 
xyz
*get predicted probs, RR ratios
mlogit presvote_dv discpresvote_dv5 discpresvote_dv6 discpresvote_dv7 discpresvote_dv8 l.discpresvote_dv5 l.discpresvote_dv6 l.discpresvote_dv7 l.discpresvote_dv8 l1.v5d_econpastbrazil l1.v81_lulagovpresapprove l1.partyintensePSDB l1.partyintense_baseline l.issues5 ///
l.presvote_dv5 l.presvote_dv6 l.presvote_dv7 l.presvote_dv8  city if wave==6 & presvote_dv<16 & discpresvote==l.discpresvote, cluster(id) base(1) 

*Lula to Lula
margins, at(discpresvote_dv5=0 discpresvote_dv6=0 discpresvote_dv7=0 discpresvote_dv8=0 l.discpresvote_dv5=0 l.discpresvote_dv6=0 l.discpresvote_dv7=0 l.discpresvote_dv8=0) atmeans predict(outcome(1)) predict(outcome(10)) predict(outcome(12)) 

recode presvote_dv 10=1 1=0 else=., gen(lula_alck)
recode presvote_dv 12=1 1=0 else=., gen(lula_helena)

*Instrumental Vars
ivregress 2sls  lula_alck l1.v5d_econpastbrazil l1.v81_lulagovpresapprove l1.partyintensePSDB l1.partyintense_baseline l.issues5 ib1.l.presvote_dv /// 
city (ib1.discpresvote_dv=ib1.l.discpresvote_dv) if wave==6, cluster(id)

ivregress 2sls  lula_helena l1.v5d_econpastbrazil l1.v81_lulagovpresapprove l1.partyintensePSDB l1.partyintense_baseline l.issues5 ib1.l.presvote_dv /// 
city (ib1.discpresvote_dv=ib1.l.discpresvote_dv) if wave==6, cluster(id)  

*Friends only, for environmental confounding
eststo clear
eststo: mlogit presvote_dv ib1.discpresvote_dv ib1.l.discpresvote_dv l1.v5d_econpastbrazil l1.v81_lulagovpresapprove l1.partyintensePSDB l1.partyintense_baseline l.issues5 ib1.l.presvote_dv ///
city if wave==6 & presvote_dv<16 & l.discfamily==0, cluster(id) base(1) 

eststo: mlogit presvote_dv ib1.discpresvote_dv l1.v5d_econpastbrazil l1.v81_lulagovpresapprove l1.partyintensePSDB l1.partyintense_baseline l.issues5 ib1.l.presvote_dv ///
city if wave==6 & presvote_dv<16 & discpresvote==l.discpresvote & l.discfamily==0, cluster(id) base(1) 

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
esttab est1 est2 using Brazil2006dyadicFRIENDS.rtf, label b(3) replace se star(* 0.05 ) nogap onecell
eststo clear

